﻿using System;
using log4net;
using log4net.Config;
using System.IO;
using System.Reflection;
namespace ExtensionTools
{
    public static class LogHelper
    {
        private static readonly ILog log;
        static LogHelper()
        {
            try
            {
                // 启用 log4net 内部调试日志（关键！）
                log4net.Util.LogLog.InternalDebugging = true;
                log4net.Util.LogLog.QuietMode = false;

                // 检查配置文件是否存在
                string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
                if (!File.Exists(configPath))
                {
                    throw new FileNotFoundException($"log4net.config 文件未找到！路径：{configPath}");
                }

                // 加载配置
                var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
                XmlConfigurator.Configure(logRepository, new FileInfo(configPath));
                log = LogManager.GetLogger(typeof(LogHelper));

                // 输出初始化日志
                log.Info("=== log4net 初始化成功 ===");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"日志初始化失败: {ex}");
                throw;
            }
        }
        public static void Info(string message)
        {
            log.Info(message);
        }

        public static void Error(string message, Exception ex = null)
        {
            if (ex != null)
            {
                log.Error(message, ex);
            }
            else
            {
                log.Error(message);
            }
        }

        public static void Warn(string message)
        {
            log.Warn(message);
        }

        public static void Debug(string message)
        {
            log.Debug(message);
        }
    }
}
